// db.js
//const mysql = require('mysql2/promise');
//require('dotenv').config(); // 引入环境变量

//const pool = mysql.createPool({
 // host: process.env.DB_HOST,
 // user: process.env.DB_USER,
 // password: process.env.DB_PASSWORD,
 // database: process.env.DB_NAME,
 // waitForConnections: true,
 // connectionLimit: 10,
 // queueLimit: 0
//});

//module.exports = pool;




const { Sequelize } = require('sequelize');
const oracledb = require('oracledb');
require('dotenv').config(); // 加载 .env 文件

// 读取环境变量
const {
  ORACLE_USER,
  ORACLE_PASSWORD,
  ORACLE_HOST,
  ORACLE_PORT,
  ORACLE_SID,
  ORACLE_INSTANT_CLIENT_DIR,
} = process.env;

// 初始化 Oracle 客户端
if (!ORACLE_INSTANT_CLIENT_DIR) {
  throw new Error('环境变量 ORACLE_INSTANT_CLIENT_DIR 未设置');
}
oracledb.initOracleClient({ libDir: ORACLE_INSTANT_CLIENT_DIR });

// 配置 Sequelize
const sequelize = new Sequelize({
  dialect: 'oracle',
  username: ORACLE_USER,
  password: ORACLE_PASSWORD,
  host: ORACLE_HOST,
  port: parseInt(ORACLE_PORT, 10),
  dialectOptions: {
    connectString: `${ORACLE_HOST}:${ORACLE_PORT}/${ORACLE_SID}`,
  },
});

// 测试连接
(async () => {
  try {
    await sequelize.authenticate();
    console.log('Oracle 连接成功！');
  } catch (error) {
    console.error('Oracle 连接失败:', error.message);
  }
})();

module.exports = sequelize;